﻿#include "regist.h"
#include "ui_regist.h"
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QMessageBox>

//字符编码设为utf-8
//#pragma execution_character_set("utf-8")

regist::regist(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::regist)
{
    //设置无边框
    this->setWindowFlags(Qt::FramelessWindowHint);
    //关闭窗口，即销毁窗体
    this->setAttribute(Qt::WA_DeleteOnClose, true);

    ui->setupUi(this);
    //背景图片等比缩放
//    QPalette PAllbackground = this->palette();
//    QImage ImgAllbackground(":/images/back.png");
//    QImage fitimgpic=ImgAllbackground.scaled(this->width(),this->height(), Qt::IgnoreAspectRatio);
//    PAllbackground.setBrush(QPalette::Window, QBrush(fitimgpic));
//    this->setPalette(PAllbackground);
    //设置窗口图标
//    this->setWindowIcon(QIcon(":/images/软件图标.png"));
    //界面窗口固定
    this->setFixedSize(819,458);
}

regist::~regist()
{
    delete ui;
}

void regist::on_pushButton_regist_clicked()
{
    QString name = ui->lineEdit_username->text();
    QString passwd = ui->lineEdit_password->text();
    QString passwd2 = ui->lineEdit_password2->text();
    //注册功能...
    QSqlTableModel model;
    model.setTable("user");
    model.setFilter(tr("user_name = '%1'").arg(name));
    if(model.select())
    {
        QMessageBox::warning(NULL, QStringLiteral("注册失败"), QStringLiteral("该用户已存在！"));
    }
    else if(QString::compare(passwd,passwd2)!=0)
    {
        QMessageBox::warning(NULL,QStringLiteral("注册失败"),QStringLiteral("两次密码输入不相同！"));
    }
    else
    {
        QSqlQuery query;
        QString insert = QString("insert into user(user_name,password) values('%1','%2')").arg(name).arg(passwd);
        if(query.exec(insert))
            QMessageBox::information(this, QStringLiteral("注册成功"), QStringLiteral("注册成功！"));
    }
}

void regist::on_pushButton_cacel_clicked()
{
    emit sendsignal();
    this->close();
}
